package com.example.finalsystem.Service;

import com.example.finalsystem.Dao.UserMapper;
import com.example.finalsystem.Entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.InputStream;

@Service
public class LoginService {
    private static   SqlSession openSession()
    {
        String resource = "mybatis-config.xml";
        InputStream is = LoginService.class.getClassLoader().getResourceAsStream(resource);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        return   sessionFactory.openSession(true);
    }
    public String userLogin(String userId,String userPassword)
    {
        SqlSession sqlSession = openSession();
        Long st = System.currentTimeMillis();
        User user = sqlSession.getMapper(UserMapper.class).getUserByUserId(userId);
        Long ed =System.currentTimeMillis();
        System.out.println("Service:" + (ed -st));
        sqlSession.close();
        if (user==null)
        {
            return "该用户还未注册";
        }
        if (!user.getUserpassword().equals(userPassword))
        {
            return "密码不正确";
        }

        return "登录成功";
    }
    public User getUserInfo(String userId)
    {
        SqlSession sqlSession = openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user =userMapper.selectByPrimaryKey(userId);
        sqlSession.close();
        return user;
    }
}
